<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{path:'home'}">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col :span="6">
          <el-input placeholder="请输入查询关键字" clearable @clear="claerInput" v-model="queryOrder.query">
            <el-button slot="append" @click="clickQueryOrder" class="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <el-table :data="orderList" border>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="订单编号" width="320px" prop="order_number"></el-table-column>
        <el-table-column label="订单价格" prop="order_price"></el-table-column>
        <el-table-column label="是否付款">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.pay_status === '1'">已付款</el-tag>
            <el-tag v-else-if="scope.row.pay_status === '0'" type="danger">未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column width="80px" label="是否发货">
          <template slot-scope="scope">{{scope.row.is_send}}</template>
        </el-table-column>
        <el-table-column label="下单时间" prop=" create ">
          <template slot-scope="scope">{{scope.row.create_time | formateDate('yyyy-MM-dd hh:mm')}}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-tooltip effect="dark" content="修改收货地址" placement="bottom">
              <el-button type="primary" @click="showEditPosDialog" size="mini" icon="el-icon-edit"></el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="查看物流进度" placement="top">
              <el-button
                type="success"
                @click="showTransportDialog(scope.row.user_id)"
                size="mini"
                icon="el-icon-location"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryOrder.pagenum"
        :page-size="queryOrder.pagesize"
        :page-sizes="[1,5,10,15]"
        :total="total"
        layout="total,sizes,prev,pager,next,jumper"
      ></el-pagination>
    </el-card>
    <el-dialog
      @close="closeEditPosDialog"
      title="修改收货地址"
      :visible.sync="isEditPosDialog"
      width="40%"
    >
      <el-form
        label-width="90px"
        :model="editPosForm"
        ref="editPosFromRef"
        :rules="editPosFormRule"
      >
        <el-form-item label="省市区/县" prop="area">
          <el-cascader v-model="editPosForm.area" :options="cityData"></el-cascader>
        </el-form-item>
        <el-form-item label="详情地址" prop="address">
          <el-input v-model="editPosForm.address"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isEditPosDialog = false">取消</el-button>
        <el-button type="primary">确认</el-button>
      </span>
    </el-dialog>
    <el-dialog title="物流进度" :visible.sync="isTransportDialog" width="40%">
      <el-timeline>
          <el-timeline-item v-for="(item,i) in transportInfo" :key="i" :timestamp="item.time">{{item.context}}</el-timeline-item>
      </el-timeline>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isTransportDialog = false">取消</el-button>
        <el-button @click="isTransportDialog = false" type="primary">确认</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import cityData from './citydata'
export default {
  data() {
    return {
      // 查询订单数据
      queryOrder: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      cityData,
      total: 0,
      // 订单数据列表
      orderList: [],
      // 是否显示编辑收货地址对话框
      isEditPosDialog: false,
      //
      editPosForm: {
        area: [],
        address: ''
      },
      editPosFormRule: {
        area: [{ required: true, message: '省市区不能为空', trigger: 'blur' }],
        address: [
          { required: true, message: '详细地址不能为空', trigger: 'blur' }
        ]
      },
      // 物流信息
      transportInfo: [],
      isTransportDialog: false
    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    // 显示物流信息对话框
    showTransportDialog(id) {
      this.isTransportDialog = true
      this.getTransportStatus(id)
    },
    // 确认修改
    confirmEditPos() {
      this.$refs.editPosFormRef.validate(valid => {
        if (!valid) return false
      })
    },
    // 发送修改收货地址请求
    async editPos() {},
    // 关闭对话框
    closeEditPosDialog() {
      this.$refs.editPosFormRef.resetField()
    },
    // 显示对话框
    showEditPosDialog() {
      this.isEditPosDialog = true
    },
    // 清空输入框内容
    claerInput() {
      this.getOrderList()
    },
    // 点击查询订单列表
    clickQueryOrder() {
      this.getOrderList()
    },
    //  每页条数发生改变
    handleSizeChange(size) {
      this.queryOrder.pagesize = size
      this.getOrderList()
    },
    // 当前页数发生改变
    handleCurrentChange(num) {
      this.queryOrder.pagenum = num
      this.getOrderList()
    },
    // 获取物流状态
    async getTransportStatus(id) {
      const { data: res } = await this.$http.get('/kuaidi/1106975712662')
      if (res.meta.status !== 200) {
        return this.$message.error('获取物流状态失败')
      }
      this.transportInfo = res.data
    },
    // 获取订单列表
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryOrder
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取订单列表失败')
      }
      this.orderList = res.data.goods
      this.total = res.data.total
    }
  }
}
</script>
<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
